package com.shop.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.shop.dao.IAdmin;
import com.shop.db.DBUtil;
import com.shop.model.Admin;

public class AdminDao implements IAdmin{

	/**
	 * 将账号密码插入数据库
	 */
	@Override
	public Integer insert(Admin a) {
		Connection conn = DBUtil.getConn();
		PreparedStatement ptmt = null;
		String sql = "INSERT INTO admin (acc,pwd) VALUES(?,?)";
		try {
			ptmt = conn.prepareStatement(sql);
			ptmt.setString(1, a.getAcc());
			ptmt.setString(2, a.getPwd());

			int i = ptmt.executeUpdate();
			return i;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (ptmt != null) {
				try {
					ptmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}
		return null;
	}
	
	
	/**
	 * 根据账号acc检查，该账户是否存在
	 */
	@Override
	public Integer checkAcc(String acc) {
		Connection conn = DBUtil.getConn();
		PreparedStatement ptmt = null;
		String sql = "SELECT * FROM admin WHERE acc=?";
		try {
			ptmt = conn.prepareStatement(sql);
			ptmt.setString(1, acc);
			ResultSet resultSet = ptmt.executeQuery();
			while (resultSet.next()) {
				return 1;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (ptmt != null) {
				try {
					ptmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}
		return 0;
	}

	//核实账号密码是否正确
	@Override
	public Integer checkExists(String acc,String pwd) {
		// 
		Connection conn = DBUtil.getConn();
		PreparedStatement ptmt = null;
		String sql = "SELECT * FROM admin WHERE acc=? AND pwd=?";
		try {
			ptmt = conn.prepareStatement(sql);
			ptmt.setString(1, acc);
			ptmt.setString(2, pwd);
			
			ResultSet resultSet = ptmt.executeQuery();
			while (resultSet.next()) {
				return 1;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (ptmt != null) {
				try {
					ptmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}
		return 0;
	}

	

}
